<!-- 天气组件 -->
<template>
  <div class="weather-container">
    <van-icon :name="getWeatherIcon(weatherInfo.weather_type)" class="weather-icon" />
    <div class="weather-info">
      <span class="temperature">{{ weatherInfo.temperature }}°C</span>
      <span class="weather">{{ weatherInfo.weather_type }}</span>
      <span class="city">{{ weatherInfo.city }}</span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import api from '@/api'

const weatherInfo = ref({
  city: '加载中...',
  weather_type: '晴',
  temperature: '--'
})

// 获取天气图标
const getWeatherIcon = (weather) => {
  const weatherMap = {
    '晴': 'sunny-o',
    '多云': 'cloud-o',
    '阴': 'cloud-o',
    '小雨': 'weather-o',
    '中雨': 'weather-o',
    '大雨': 'weather-o',
    '雷阵雨': 'weather-o',
    '雪': 'weather',
  }
  return weatherMap[weather] || 'weather-o'
}

// 获取天气数据
const getWeatherInfo = async () => {
  try {
    const res = await api.weather.get()
    weatherInfo.value = res.data
  } catch (error) {
    console.error('获取天气信息失败:', error)
  }
}

onMounted(() => {
  getWeatherInfo()
})
</script>

<style lang="scss" scoped>
.weather-container {
  display: flex;
  align-items: center;
  padding: 0 8px;
  height: 100%;
  color: #323233;
  
  .weather-icon {
    font-size: 24px;
    margin-right: 8px;
  }
  
  .weather-info {
    display: flex;
    align-items: center;
    font-size: 14px;
    
    .temperature {
      margin-right: 4px;
      font-weight: 500;
    }
    
    .weather {
      margin-right: 4px;
      color: #666;
    }
    
    .city {
      color: #999;
      font-size: 12px;
    }
  }
}
</style> 